<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

$fromversion = $_GET['fromversion'];
$fromversion = str_replace('v', '', $_GET['fromversion']);

if($fromversion < 1.1) {
    $config = 'pre_er_kami_buy_config';
    $kami_buy_table = 'pre_er_kami_buy';
    
    $check_sql = "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='$config' AND COLUMN_NAME='type';";
    $row = DB::fetch_first($check_sql);

    if($row['COUNT(*)'] == 0) {
        // Column doesn't exist, add it
        $sql = "ALTER TABLE `$config` ADD `type` VARCHAR(20) NOT NULL DEFAULT 'site';";
        runquery($sql);
    }

    // 插入新数据
    $sql = <<<EOF
    INSERT INTO `$config` (`type`, `key`, `name`, `value`, `desc`) VALUES
    ('api_site', 'kami_api', '是否请求第三方获取卡密', '0', '1是、0否'),
    ('api_site', 'kami_api_url', '卡密分类列表获取地址', '', ''),
    ('api_site', 'kami_api_get_url', '卡密获取地址', '', ''),
    ('api_site', 'kami_api_private_key', '私钥', '', '设置后不再显示，用于解密发回来的数据');
    EOF;
    runquery($sql);

    // 创建新表
    $sql = <<<EOF
    DROP TABLE IF EXISTS `pre_er_kami_buyforapi`;
    create table pre_er_kami_buyforapi
    (
        id          int auto_increment
            primary key,
        uid         int                default 0                    null comment '用户id',
        title       varchar(255)                                    null comment '',
        `desc`      varchar(255)                                    null comment '描述',
        kami_key    varchar(255)                                    null comment '卡密',
        order_no    varchar(255)                                    null comment '第三方订单号',
        extcredits  int                default 0                    null comment '积分分类',
        credit      int                default 0                    null comment '消耗积分值',
        create_time int                null comment '创建时间',
        update_time int                null comment '更新时间',
        expire_time int                null comment '过期时间，空或0表示永不过期',
    )
        engine = InnoDB;
    EOF;
    runquery($sql);
}

$finish = TRUE;